package com.oracle.controller;

import com.oracle.Service.impl.CategoryService;
import com.oracle.pojo.Category;
import javafx.concurrent.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/add")
    public String add(Category category, HttpSession Session){
        String result = categoryService.insertByCategory(category);
        if(result.equals("NO")){
            Session.setAttribute("categoryAddInfo","类型名或者描述不能为空");
        }
        if (result.equals("YES")) {
            Session.setAttribute("categoryAddInfo", "添加成功！");
        }
        return "redirect:/adminCategoryView";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody Category category){
        Map<String,Object> map = new HashMap<String,Object>();
        //判断category是否为NULL值
        //判断category的cid是否为空值
        if (category==null||category.getCid()==null||category.getCid()==0){
            //传输的数据不能为null
            map.put("updateInfo",200);
            return map;
        }
        if (category.getCname()==null||category.getCname().equals("")){
            //返回201表示类型名不能为空
            map.put("updateInfo",201);
            return map;
        }
        if (category.getDescription()==null||category.getDescription().equals("")){
            //返回201表示描述不能为空
            map.put("updateInfo",201);
            return map;
        }
        String result = categoryService.updateByCid(category);
        if (result.equals("NO")){
            //500表示没有修改成功
            map.put("updateInfo",500);
            return map;
        }
        //100成功
        map.put("updateInfo",100);
        map.put("cname",category.getCname());
        map.put("description",category.getDescription());
        return map;
    }

//    @RequestMapping("/delete")
//    @ResponseBody
//    public Map<String,Object> delete(@RequestBody Category category){
//        Map<String,Object> map = new HashMap<String,Object>();
//        //当category的值为null时，返回200
//        if (category==null){
//            map.put("deleteInfo",200);
//            return map;
//        }
//        if (category.getCid()==null){
//            map.put("deleteInfo",200);
//            return map;
//        }
//        String result = categoryService.deleteByPrimaryKey(category);
//        if (result.equals("NO")){
//            map.put("updateInfo",201);
//            return map;
//        }
//        //删除成功
//        map.put("deleteInfo",100);
//        return map;
//    }

}
